package com.winning.sx.microframework.dao.sysmanage;

import com.winning.dapi.data.api.IDataAccessService;
import com.winning.dapi.data.entity.DataTable;
import com.winning.sx.microframework.common.constant.GenericApiConstant;
import com.winning.sx.microframework.common.util.Constains;
import com.winning.sx.microframework.common.util.SqlTool;
import com.winning.sx.microframework.model.genericapi.*;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wangj_envy
 * @date 2019年6月5日15:29:39
 */
@Repository
public class GenericApiManageDao {
    @Autowired
    @Qualifier("dataSource")
    private IDataAccessService dataAccessService;
//logLevel,interfaceId,interfaceName,interfacePattern,visitAddress,sendContent,returnContent,keyWord1,exceptionMessage,createTime,respondTime
//    :logLevel,:interfaceId,:interfaceName,:interfacePattern,:visitAddress,:sendContent,:returnContent,:keyWord1,:exceptionMessage,:createTime,:respondTime
    public int recordLog(GenericApiInterfaceLog log) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {

        dataAccessService.executeSqlUsingParam("delete from SYS_InterfaceLog where id<(" +
                " select max(id)-10000 from SYS_InterfaceLog" +
                ")",new HashMap<>());

        String sql = "Insert into SYS_InterfaceLog " +
                " (logLevel,interfaceId,interfaceName,interfacePattern,visitAddress,sendContent,returnContent,keyWord1,exceptionMessage,createTime,updateTime,validFlag) " +
                " Values (:logLevel,:interfaceId,:interfaceName,:interfacePattern,:visitAddress,:sendContent,:returnContent,:keyWord1,:exceptionMessage,"+ SqlTool.getSysTime()+","+SqlTool.getSysTime()+","+
        Constains.SYS_VALIDFLAG_TRUE+")";
        Map map = BeanUtils.describe(log);
        map.remove("paramMap");
        int i = dataAccessService.executeSqlUsingParam(sql, map);
        return i;
    }

    public GenericApiServerInfo queryServer(int server) {
        Map<String, Object> map = new HashedMap();
        map.put("id",server);
        String sql = "SELECT * FROM SYS_ServerInfo WHERE id = :id";
        List<GenericApiServerInfo> genericApiServerInfos = dataAccessService.querySqlUsingParamForBean(sql, GenericApiServerInfo.class, map);
        return genericApiServerInfos.get(0);
    }
    public List queryServer(String code) {
        Map<String, Object> map = new HashedMap();
        map.put("code",code);
        String sql = "SELECT * FROM SYS_ServerInfo s left join SYS_ServerConfig c on s.id=c.serverId WHERE s.code = :code";
        DataTable data = dataAccessService.querySqlUsingParam(sql, map);
        return data.getDataRows();
    }
    public GenericApiInterfaceInfo queryInterface(String interfaceCode) {
        Map<String, Object> map = new HashedMap();
        map.put("id",interfaceCode);
        String sql = "SELECT * FROM SYS_InterfaceInfo WHERE id = :id";
        List<GenericApiInterfaceInfo> GenericApiInterfaceInfos = dataAccessService.querySqlUsingParamForBean(sql, GenericApiInterfaceInfo.class, map);
        return GenericApiInterfaceInfos.get(0);
    }

    public List<GenericApiInterfaceConfig> queryInterfacePropties(int interfaceCode) {
        Map<String, Object> map = new HashedMap();
        map.put("interfaceCode",interfaceCode);
        map.put("validFlag", Constains.SYS_VALIDFLAG_TRUE);
        String sql = "SELECT * FROM SYS_InterfaceConfig WHERE interfaceId = :interfaceCode and validFlag=:validFlag";
        List<GenericApiInterfaceConfig> genericApiInterfaceConfigs = dataAccessService.querySqlUsingParamForBean(sql, GenericApiInterfaceConfig.class, map);
        return genericApiInterfaceConfigs;
    }
}
